package net.forsalemall.android;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.utils.Constants;
import com.utils.HttpUtils;
import com.utils.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;

public class SettingsActivity extends Activity {
	private Button unregisterButton;
	private SharedPreferences sharedPreference;

	private String country;
	private boolean isRegister;

	
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.settings);
			isRegister = getIntent().getBooleanExtra(Constants.KEY_IS_REGISTER, false);
			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(this);
			country = sharedPreference.getString(Constants.KEY_COUNTRY, null);
			
			final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
			actionBar.setTitle(getString(R.string.title_settings));
			actionBar.setHomeAction(new Action() {
				
				public void performAction(View view) {
					backToHome();
				}

				
				public int getDrawable() {
					// TODO Auto-generated method stub
					return R.drawable.ic_title_home_default;
				}
			});
			actionBar.setDisplayHomeAsUpEnabled(true);

			unregisterButton = (Button) findViewById(R.id.button_ungrgister);
			if(!isRegister){
				unregisterButton.setEnabled(false);
			}
			unregisterButton.setOnClickListener(
					new View.OnClickListener() {
						
						public void onClick(View v) {
							showUnregisterConfirmation();
						}
					});
			findViewById(R.id.button_change_country).setOnClickListener(
					new View.OnClickListener() {
						
						public void onClick(View v) {
							setCountry();
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void backToHome(){
		try {
			startActivity(new Intent(this, HomeActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void setCountry() {
		try {
			final Dialog dialog = new Dialog(this);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(LayoutInflater.from(this).inflate(
					R.layout.country_dialog, null));
			dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT);
			dialog.setCancelable(false);

			Button okButton = (Button) dialog.findViewById(R.id.button_ok);
			final RadioButton optionKuwait = (RadioButton) dialog
					.findViewById(R.id.radio_kuwait);
			final RadioButton optionUAE = (RadioButton) dialog
					.findViewById(R.id.radio_uae);
			if (Constants.COUNTRY_UAE.equals(country)) {
				optionKuwait.setChecked(false);
				optionUAE.setChecked(true);
			} else {
				optionUAE.setChecked(false);
				optionKuwait.setChecked(true);
			}
			okButton.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					if (optionUAE.isChecked()) {
						sharedPreference
								.edit()
								.putString(Constants.KEY_COUNTRY,
										Constants.COUNTRY_UAE).commit();
						country = Constants.COUNTRY_UAE;
					} else {
						sharedPreference
								.edit()
								.putString(Constants.KEY_COUNTRY,
										Constants.COUNTRY_KUWAIT).commit();
						country = Constants.COUNTRY_KUWAIT;
					}
					dialog.dismiss();
				}
			});
			dialog.show();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void showUnregisterConfirmation() {
		try {
			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(getString(R.string.unregister_confirmation))
					.setCancelable(false)
					.setPositiveButton(getString(R.string.yes),
							new DialogInterface.OnClickListener() {
								public void onClick(
										final DialogInterface dialog,
										final int id) {
									unregister();
								}
							})
					.setNegativeButton(getString(R.string.no),
							new DialogInterface.OnClickListener() {
								public void onClick(
										final DialogInterface dialog,
										final int id) {

								}
							});
			final AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void unregister() {
		try {
			new AsyncTask<String, String, String>() {
				private ProgressDialog progressDialog;

				
				protected void onPreExecute() {
					try {
						progressDialog = Utils.getProgressDialog(
								SettingsActivity.this, null,
								getString(R.string.loading));
						progressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				
				protected String doInBackground(String... arg0) {
					try {
						HttpUtils.unregisterDevice(Utils
								.getDeviceId(SettingsActivity.this));
						sharedPreference.edit().putLong(Constants.KEY_REGISTER_NOTIFICATION,System.currentTimeMillis()).commit();
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				
				protected void onPostExecute(String unused) {
					try {
						unregisterButton.setEnabled(false);
						progressDialog.dismiss();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
